<?php

use plugin\saiadmin\app\logic\system\SystemConfigLogic;
use plugin\saiadmin\utils\Arr;
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;

/**
 * Here is your custom functions.
 */

//  递归创建文件夹并下载文件
function createFoldersRecursively($foldersArray, $basePath)
{
    foreach ($foldersArray as $key => $value) {
        if (is_array($value)) {
            $newBasePath = rtrim($basePath, '/') . '/' . $key;
            if (!file_exists($newBasePath)) {
                mkdir($newBasePath, 0755, true);
            }
            createFoldersRecursively($value, $newBasePath);
        } else {
            $folderPath = rtrim($basePath, '/') . '/' . $key;
            if (!file_exists($folderPath)) {
                mkdir($folderPath, 0755);
            }
        }
    }
}


// 远程下载七牛云文件
// 七牛
// $config = [
//     'adapter' => \Tinywan\Storage\Adapter\QiniuAdapter::class,
//     'accessKey' => Arr::getConfigValue($uploadConfig, 'qiniu_accessKey'),
//     'secretKey' => Arr::getConfigValue($uploadConfig, 'qiniu_secretKey'),
//     'bucket' => Arr::getConfigValue($uploadConfig, 'qiniu_bucket'),
//     'dirname' => Arr::getConfigValue($uploadConfig, 'qiniu_dirname'),
//     'domain' => Arr::getConfigValue($uploadConfig, 'qiniu_domain'),
// ];
function downloadFromQiniu($qiniuKey, $localFilePath)
{

    $logic = new SystemConfigLogic();
    $uploadConfig = $logic->getGroup('upload_config');

    // 配置七牛云信息
    $accessKey = Arr::getConfigValue($uploadConfig, 'qiniu_accessKey');
    $secretKey = Arr::getConfigValue($uploadConfig, 'qiniu_secretKey');
    $bucket = Arr::getConfigValue($uploadConfig, 'qiniu_bucket');

    // 初始化鉴权对象
    $auth = new Auth($accessKey, $secretKey);
    $bucketManager = new BucketManager($auth);

    list($ret, $err) = $bucketManager->fetch($qiniuKey, $bucket, $localFilePath);
    if ($err !== null) {
        echo "下载文件出错: " . $err->message() . "\n";
    } else {
        echo "文件下载成功\n";
    }
}

// 上传七牛云
function uploadToQiniu($localZipFilePath, $qiniuZipKey)
{
    $logic = new SystemConfigLogic();
    $uploadConfig = $logic->getGroup('upload_config');

    // 配置七牛云信息
    $accessKey = Arr::getConfigValue($uploadConfig, 'qiniu_accessKey');
    $secretKey = Arr::getConfigValue($uploadConfig, 'qiniu_secretKey');
    $bucket = Arr::getConfigValue($uploadConfig, 'qiniu_bucket');

    // 初始化鉴权对象
    $auth = new Auth($accessKey, $secretKey);

    $token = $auth->uploadToken($bucket);
    $uploadManager = new UploadManager();
    list($ret, $err) = $uploadManager->putFile($token, $qiniuZipKey, $localZipFilePath);
    if ($err !== null) {
        echo "上传文件到七牛云出错: " . $err->message() . "\n";
    } else {
        echo "文件上传到七牛云成功\n";
    }
}



// 生成ZIP压缩包
function createZip($sourcePath, $zipFileName)
{
    $zip = new ZipArchive();
    if ($zip->open($zipFileName, ZipArchive::CREATE) === true) {
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($sourcePath),
            RecursiveIteratorIterator::SELF_FIRST
        );

        foreach ($files as $file) {
            $filePath = $file->getRealPath();
            $relativePath = substr($filePath, strlen($sourcePath) + 1);
            if ($file->isDir()) {
                $zip->addEmptyDir($relativePath);
            } else {
                $zip->addFile($filePath, $relativePath);
            }
        }

        $zip->close();
        echo "ZIP压缩包创建成功\n";
    } else {
        echo "创建ZIP压缩包失败\n";
    }
}
